﻿using Base.Structure.Infrastructure.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Base.Structure.Infrastructure.Repositories
{
    public class UserRepository : IUserRepository
    {
        private readonly IFreeSql _freeSql;

        public UserRepository(IFreeSql freeSql)
        {
            _freeSql = freeSql;
        }

        // 获取所有用户
        public async Task<List<User>> GetAllUsersAsync()
        {
            return await _freeSql.Select<User>().ToListAsync();
        }

        // 获取单个用户
        public async Task<User> GetUserByIdAsync(int id)
        {
            return await _freeSql.Select<User>().Where(u => u.Id == id).FirstAsync();
        }

        // 添加用户
        public async Task<int> AddUserAsync(User user)
        {
            return await _freeSql.Insert(user).ExecuteAffrowsAsync();
        }

        // 更新用户
        public async Task<int> UpdateUserAsync(User user)
        {
            return await _freeSql.Update<User>()
                                  .SetSource(user)
                                  .ExecuteAffrowsAsync();
        }

        // 删除用户
        public async Task<int> DeleteUserAsync(int id)
        {
            return await _freeSql.Delete<User>().Where(u => u.Id == id).ExecuteAffrowsAsync();
        }
    }
}
